﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.Composition;
using System.Reflection;
using RiaToDalLinq.Contracts;
using RiaToDalLinq.Enumeration;

namespace RIAServicesLegacyDalIntegration.Web
{
    [Export(typeof(IDalResolver))]
    public class CustomDalResolver : IDalResolver
    {
        /// <summary>
        /// Gets a manager instance from an entity name (for example, if we are working with a User entity, it will return a UserManagerInstance)
        /// </summary>
        /// <param name="entityName"></param>
        /// <returns></returns>
        public object GetManagerInstance(string entityName)
        {
            //get the type info for the DAL provider
            Type providerType = typeof(LegacyDal.DalProvider);

            //gets a reference to the manager instance
            PropertyInfo singletonPropertyInfo = providerType.GetProperty(string.Format("{0}ManagerInstance", entityName));
            object propertyReference = singletonPropertyInfo.GetValue(LegacyDal.DalProvider.ProviderInstance, null);
            return propertyReference;
        }

        public int? GetIdFromCode(Type lookupEntityType, string idFieldName, string codeFieldName, string codeValue)
        {
            return 1;
        }

        public Tuple<string, string> WhereClauseParameter
        {
            get
            {
                return new Tuple<string, string>("whereClause", string.Empty);
            }
        }

        public Tuple<string, string> OrderByParameter
        {
            get
            {
                return new Tuple<string, string>("orderBy", string.Empty);
            }
        }

        public Tuple<string, int> PageStartParameter
        {
            get
            {
                return new Tuple<string, int>("pageIndex", 0);
            }
        }

        public Tuple<string, int> PageSizeParameter
        {
            get
            {
                return new Tuple<string, int>("pageSize", 25);
            }
        }

        public Tuple<string, int> RowCountParameter
        {
            get
            {
                return new Tuple<string, int>("rowCount", 0);
            }
        }

        public PageStartActAs PageStartActingAs
        {
            get
            {
                return RiaToDalLinq.Enumeration.PageStartActAs.PageIndex;
            }
        }
    }
}